﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiDrawingWinform.Commands;
using MediatR;

namespace AiDrawingWinform
{
    public class DrawingRequestHandler : IRequestHandler<ClearCanvasCommand>, 
        IRequestHandler<DrawPixelCommand>,
        IRequestHandler<DrawHorizontalLineCommand>,
        IRequestHandler<DrawVerticalLineCommand>
    {

        #region Public Methods

        public Task Handle(ClearCanvasCommand request, CancellationToken cancellationToken)
        {
            Application
                .OpenForms
                .OfType<FrmMain>()
                .First()
                .DrawCanvas();

            return Task.CompletedTask;
        }

        public Task Handle(DrawPixelCommand request, CancellationToken cancellationToken)
        {
            Application
                .OpenForms
                .OfType<FrmMain>()
                .First()
                .DrawPixel(request.X, request.Y, request.Color);

            return Task.CompletedTask;
        }

        public Task Handle(DrawHorizontalLineCommand request, CancellationToken cancellationToken)
        {
            var frmMain = Application
                .OpenForms
                .OfType<FrmMain>()
                .First();

            for (var x = request.X1; x <= request.X2; x++)
            {
                frmMain.DrawPixel(x, request.Y, request.Color);
            }

            return Task.CompletedTask;
        }

        public Task Handle(DrawVerticalLineCommand request, CancellationToken cancellationToken)
        {
            var frmMain = Application
                .OpenForms
                .OfType<FrmMain>()
                .First();
            for (var y = request.Y1; y <= request.Y2; y++)
            {
                frmMain.DrawPixel(request.X, y, request.Color);
            }

            return Task.CompletedTask;
        }

        #endregion Public Methods
    }
}